package com.putao.album.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.putao.album.R;
import com.putao.album.util.ViewHelper;

/**
 * Created by jidongdong on 15/5/7.
 */
public class LoadingDialogBuilder extends DialogBuilder {
    AnimatorSet animation;
    View mContetView;
    WindowManager mWindowManger;
    boolean mFlagAdded = false;

    public LoadingDialogBuilder(Context context) {
        super(context);
        mWindowManger = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        initContentView();
    }

    void initContentView() {
        mContetView = LayoutInflater.from(context).inflate(getContentViewId(), null);
        ImageView img_loading = ViewHelper.queryViewById(mContetView, R.id.img_loading);
        ObjectAnimator rotation_ani = ObjectAnimator.ofFloat(img_loading, "rotation", 0, 360);
        rotation_ani.setDuration(500);
        rotation_ani.setRepeatCount(-1);
        rotation_ani.setInterpolator(new LinearInterpolator());
        animation = new AnimatorSet();
        animation.play(rotation_ani);
    }

    public void Show() {
        if (!mFlagAdded) {
            WindowManager.LayoutParams para = new WindowManager.LayoutParams();
            para.height = -1;
            para.width = -1;
            para.format = 1;
            para.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
            para.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            if (mContetView == null) {
                initContentView();
            }
            mWindowManger.addView(mContetView, para);
            startAnimation();
            mFlagAdded = true;
        }
    }

    public void startAnimation() {
        animation.start();
    }

    public void Dismiss() {
        try {
            mWindowManger.removeView(mContetView);
        } catch (Exception e) {
            mContetView = null;
            e.printStackTrace();
        }
        mFlagAdded = false;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.dialog_pt_loading_layout;
    }

    @Override
    protected void doInitSubView(View view) {
    }

    @Override
    protected void doSetupData() {

    }
}
